package fr.teuteuf.runstickmanrun;

import java.io.IOException;
import java.io.StringWriter;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.HttpMethods;
import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponse;
import com.badlogic.gdx.Net.HttpResponseListener;
import com.badlogic.gdx.utils.JsonWriter;

import fr.teuteuf.runstickmanrun.screen.HomeScreen;
import fr.teuteuf.runstickmanrun.screen.AbstractScreen;
import fr.teuteuf.runstickmanrun.screen.MultiPlayerScreen;
import fr.teuteuf.runstickmanrun.screen.MultiplayerRankScreen;
import fr.teuteuf.runstickmanrun.screen.ScoreScreen;
import fr.teuteuf.runstickmanrun.screen.SinglePlayerScreen;

public class RunStickmanRun implements ApplicationListener {
	private AbstractKeyMap keyMap;
	private boolean runningOnOuya;
	private AbstractScreen[] screens;
	private AbstractScreen currentScreen = null;
	private String username;
	private int lastScore;
	private int highScore;
	private int selectedLevel;
	
	private final int NB_SCREENS = 5;
	
	public static final int INDEX_HOME_SCREEN = 0;
	public static final int INDEX_SINGLEPLAYER_SCREEN = 1;
	public static final int INDEX_SCORE_SCREEN = 2;
	public static final int INDEX_MULTIPLAYER_SCREEN = 3;
	public static final int INDEX_MULTIPLAYER_RANK_SCREEN = 4;
	
	
	public RunStickmanRun(AbstractKeyMap keyMap, boolean runningOnOuya, String username) {
		this.keyMap = keyMap;
		this.runningOnOuya = runningOnOuya;
		this.username = username;
		lastScore = highScore = 0;
		selectedLevel = 0;
		
		screens = new AbstractScreen[NB_SCREENS];
		for(int i = 0; i < screens.length; i++) {
			screens[i] = null;
		}
	}

	@Override
	public void create() {
		changeCurrentScreen(INDEX_HOME_SCREEN, true);
	}

	@Override
	public void dispose() {
		currentScreen.dispose();
		for(int i = 0; i < screens.length; i++) {
			try {
				screens[i].dispose();
			} catch (Exception e) {
			}
		}
	}

	@Override
	public void render() {
		currentScreen.update(Gdx.graphics.getDeltaTime());
		currentScreen.render();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
	
	public boolean isOnOuya() {
		return runningOnOuya;
	}
	
	public AbstractKeyMap getKeyMap() {
		return keyMap;
	}
	
	public void changeCurrentScreen(int indexScreen, boolean reinit) {
		
		if(currentScreen != null) {
			currentScreen.putBackground();
		}
		
		if(screens[indexScreen] == null) {
			switch(indexScreen) {
				case INDEX_HOME_SCREEN:
					screens[indexScreen] = new HomeScreen(this);
					break;
				case INDEX_SINGLEPLAYER_SCREEN:
					screens[indexScreen] = new SinglePlayerScreen(this);
					break;
				case INDEX_SCORE_SCREEN:
					screens[indexScreen] = new ScoreScreen(this);
					break;
				case INDEX_MULTIPLAYER_SCREEN:
					screens[indexScreen] = new MultiPlayerScreen(this);
					break;
				case INDEX_MULTIPLAYER_RANK_SCREEN:
					screens[indexScreen] = new MultiplayerRankScreen(this);
					break;
			}
			screens[indexScreen].create();
		} else if(reinit) {
			screens[indexScreen].reinit();
		}
		
		currentScreen = screens[indexScreen];
		currentScreen.putForeground();
	}
	
	public AbstractScreen getScreen(int indexScreen) {
		return screens[indexScreen];
	}
	
	public String getUsername() {
		return username;
	}
	
	public void addScore(int score) {
		//Clean this... use constants...
		lastScore = score;
		if(score > highScore) {
			highScore = score;
		}
		
		//Send score to cloud app:
		StringWriter jsonText = new StringWriter();
		try {
			new JsonWriter(jsonText)
				.object()
					.set("username", username)
					.set("score", score)
				.pop();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		final HttpRequest httpPost = new HttpRequest(HttpMethods.POST);
		httpPost.setUrl("http://run-stickman-run.appspot.com/scoresServlet");
		httpPost.setContent(jsonText.toString());
		Gdx.net.sendHttpRequest(httpPost, new HttpResponseListener() {
			@Override
			public void failed(Throwable t) {
			}
			@Override
			public void handleHttpResponse(HttpResponse httpResponse) {
			}
		});
	}
	
	public int getLastScore() {
		return lastScore;
	}
	
	public int getHighScore() {
		return highScore;
	}

	public int getSelectedLevel() {
		return selectedLevel;
	}
	
	public void setSelectedLevel(int selectedLevel) {
		this.selectedLevel = selectedLevel;
	}
}
